package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.UserOrderDao;
import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.CompanyProfit;
import ex.com.jm.food.entity.FoodCart;
import ex.com.jm.food.entity.Order;
import ex.com.jm.food.entity.OrderItem;
import ex.com.jm.food.entity.User;

public class UserOrderDaoImpl extends HibernateDaoSupport implements
		UserOrderDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Order> getOrders(final String userPhone,final String s_date,final String e_date,final int initPage,final int pageRow) {
		// TODO Auto-generated method stub
		Object order=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Order where userPhone='"+userPhone+"' ";
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and orderDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and orderDate<='"+s_date+"'";
				}
				hql+="order by orderDate desc";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Order>) order;
	}

	@Override
	public Object saveOrder(Order order) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(order);
	}

	@Override
	public Order getOrder(String orderId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Order.class, orderId);
	}

	@Override
	public void delOrder(String orderId) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Order.class, orderId));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<OrderItem> getOrderItems(final String orderId,final int initPage,final int pageRow) {
		// TODO Auto-generated method stub
		Object otem=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select "
						+ "new ex.com.jm.food.entity.dto.OrderItemDTO(orderItemId,"
						+ "menuId,menuName,menuNowPrice,profit,menuNum"
						+ ") from OrderItem where orderId='"+orderId+"'";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<OrderItem>) otem;
	}

	@Override
	public Object saveOrderItem(OrderItem orderItem) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(orderItem);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void delOrderItem(final String orderId) {
		// TODO Auto-generated method stub
       this.getHibernateTemplate().execute(new HibernateCallback() {

		@Override
		public Object doInHibernate(Session session) throws HibernateException,
				SQLException {
			// TODO Auto-generated method stub
			String hql="delete from OrderItem where orderId='"+orderId+"'";
			Query query=session.createQuery(hql);
			
			return query.executeUpdate();
		}
	});
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int orderCount(final String userPhone,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Order where userPhone='"+userPhone+"' ";
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and orderDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and orderDate<='"+s_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int orderItemCount(final String orderId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from OrderItem where orderId='"+orderId+"'";
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveCompanyProfit(CompanyProfit companyProfit) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(companyProfit);
	}

	@Override
	public User getUser(String userId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(User.class, userId);
	}

	@Override
	public Company getCompany(String comId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, comId);
	}

	@Override
	public FoodCart getFoodCart(String cratId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(FoodCart.class, cratId);
	}

}
